﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    /// <summary>
    /// 使用手机号码登录
    /// POST
    /// ./api/v1.1/action.ashx?sys:cmd=loginbyphone
    /// form:
    ///     op:phone=???
    ///     &op:password=???
    ///     &op:verifycode=???
    /// </summary>
    class LoginByPhoneAction : PostAction<Params.LoginByPhoneParams, Results.LoginByPhoneResult>
    {
        protected override ActionResult<Results.LoginByPhoneResult> OnExecute(ActionContext context, SysParameters sysParameters, Params.LoginByPhoneParams opParams)
        {
            AccountService service = new AccountService();
            ServiceResult<LoginResult> serviceResult = service.LoginByPhone(opParams.Phone, opParams.Password);

            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                Results.LoginByPhoneResult actionResult = new Results.LoginByPhoneResult()
                {
                    Id = serviceResult.Data.UserId,
                    Name = serviceResult.Data.Name,
                    Phone = serviceResult.Data.Phone,
                    Token = Models.UIDToken.ToToken(Models.u.build(serviceResult.Data.UserId, DateTime.Now.AddYears(100).ToString("yyyy-MM-dd HH:mm:ss"))),
                    Sex = serviceResult.Data.Sex,
                    AvatarUrl = string.Format("{0}{1}", Configs.BaseWebUrl, serviceResult.Data.AvatarUrl),
                    NickName = serviceResult.Data.NickName,
                };

                actionResult.City = serviceResult.Data.City;

                JizSport.Common.Logger.DebugToTag(LogTag.TAG, "LoginByPhone success, userid:{0}, name:{1}, phone:{2}", serviceResult.Data.UserId, serviceResult.Data.Name, serviceResult.Data.Phone);
                return new ActionResult<Results.LoginByPhoneResult>("1.0.3",
                    true,
                    ErrorCode.NoError,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            else
            {
                Results.LoginByPhoneResult actionResult = new Results.LoginByPhoneResult();
                if (serviceResult.Data != null)
                {
                    actionResult.Id = serviceResult.Data.UserId;
                    actionResult.Name = serviceResult.Data.Name;
                    actionResult.Phone = serviceResult.Data.Phone;
                    actionResult.RetryCount = serviceResult.Data.RetryCount;
                    actionResult.LockoutDuration = serviceResult.Data.LockoutDuration;
                    actionResult.Sex = serviceResult.Data.Sex;
                    actionResult.NickName = serviceResult.Data.NickName;
                    actionResult.AvatarUrl = string.Format("{0}{1}", Configs.BaseWebUrl, serviceResult.Data.AvatarUrl);
                }
                return new ActionResult<Results.LoginByPhoneResult>("1.0.3", 
                    false,
                    serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
        }
    }
}
